package com.congee02.bt.dfs.top2bottom;

import com.congee02.bt.TreeNode;

public class HasPathSum {

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        return process(root, targetSum);
    }

    private boolean process(TreeNode root, int targetSum) {

        if (root == null) {
            return false;
        }

        if (root.left == null && root.right == null) {
            return targetSum == root.val;
        }

        return process(root.left, targetSum - root.val) || process(root.right, targetSum - root.val);

    }

}
